<?php
namespace app\model;

use think\Model;
use app\model\User;

class BbsComment extends Model
{
    protected $table = "ea_bbs_comment";

    /**
     * [user 关联评论人信息]
     * @return [type] [description]
     */
    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }

    /**
     * [comment_list 是否有子评论]
     * @return [type] [description]
     */
    public function somCommentList()
    {
        return $this->hasMany(self::class, 'parent_id', 'id');
    }

     /**
     * [getCreateTimeAttr 友好显示时间]
     * @since  2022-04-06
     * @author Dai哥
     * @param  [type]     $value [description]
     * @return [type]            [description]
     */
    public function getCreateTimeAttr($value)
    {
        return get_friend_date($value);
    }

    /**
     * [getBbsCommentList 详细页面获取评论列表]
     * @since  2022-04-28
     * @author Dai哥
     * @param  [type]     $data [description]
     * @return [type]           [description]
     */
    public static function getBbsCommentList($data)
    {
        $model = self::whereNull('delete_time');

        if(isset($data['bbs_id']) && $data['bbs_id']){
            $model = $model->where('bbs_id', $data['bbs_id']);
        }

        if(isset($data['comment_id']) && $data['comment_id']){
            $model = $model->where('id', $data['comment_id']);
        }

        $model = $model->field('id, bbs_id, user_id, parent_id, msg, create_time')
            ->where('parent_id', 0)
            ->order('id DESC')
            ->with([
                'addUser',
                'ment' => function($query) use ($data){
                    $query->field('id, bbs_id, user_id, parent_id, msg, create_time')
                        ->with([
                            # 提交回复评论人的信息
                            'plUser'
                        ])
                        ->order('id DESC');
                }, 
            ]);

        $list = $model->page($data['page'], $data['limit'])->select();

        return $list;
    }

    /**
     * [ment getBbsCommentList 这个方法关联自己是是否有下级]
     * @since  2022-04-28
     * @author Dai哥
     * @return [type]     [description]
     */
    public function ment()
    {
        return $this->hasMany(self::class, 'parent_id', 'id');
    }

    /**
     * [ment getBbsCommentList 这个方法关联发布人的信息]
     * @since  2022-04-28
     * @author Dai哥
     * @return [type]     [description]
     */
    public function addUser()
    {
        return $this->hasOne(User::class, 'id', 'user_id')->field('id, nickname, avatar');
    }

    /**
     * [ment getBbsCommentList 这个方法关评论人的信息]
     * @since  2022-04-28
     * @author Dai哥
     * @return [type]     [description]
     */
    public function plUser()
    {
        return $this->hasOne(User::class, 'id', 'user_id')->field('id, nickname, avatar');
    }
}